// packageH/credit_value/detail/detail.js
import DPagination from "../DPagination";
import DHttp from "../DHttp";
const pagination = new DPagination("detailData");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentShowTab: "all",
    detailData: {},
    detailPageCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    pagination.bind(this);
    pagination.clean();
    this.loadDetailData("all");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadDetailData(this.data.currentShowTab);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  switchListTab({ detail: { name } }) {
    pagination.clean();
    this.loadDetailData(name);
    this.setData({
      detailPageCount: 0,
    });
  },
  loadDetailData(type) {
    switch (type) {
    case "all":
      this.getDetailData(null);
      break;
    case "income":
      this.getDetailData(1);
      break;
    case "expenditure":
      this.getDetailData(2);
      break;
    }
  },
  getDetailData(dataType) {
    if (pagination.loading || pagination.finished) {
      return;
    }
    pagination.loading = true;
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    const requestParams = {
      page: pagination.loadPage,
    };
    if (requestParams !== null) {
      requestParams["detail_type"] = dataType;
    }
    DHttp.request(
      "plugin.credit-inventory.frontend.controllers.detail.index",
      requestParams
    )
      .then(({ data, last_page, current_page, per_page }) => {
        pagination.loading = false;
        wx.hideLoading();
        if (last_page == current_page || data.length < per_page) {
          pagination.finished = true;
        }
        pagination.limit = per_page;
        pagination.push(data);
        this.setData({
          detailPageCount: this.data.detailPageCount + data.length,
        });
      })
      .catch(({ msg }) => {
        wx.hideLoading();
        wx.showToast({
          title: msg,
          icon: "none",
        });
        pagination.loading = false;
      });
  },
});
